<?php

namespace App\Http\Middleware;

use App\Models\RepaySummary;
use Closure;
use App\Http\Requests\Collection\BaseRequest;
use App\Library\ArrayTools\ArrayTools;
use App\Models\CollectionApiRequest;

class CheckJieFeiMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $requestData = $request->only(['sign','content']);
        $pubData = json_decode($requestData['content'],true);
        $result = $this->verifySign($pubData,$requestData['sign']);
        if(!$result){
            //验证不通过
            $returnData['result'] = CollectionApiRequest::RESPONSE_SIGN_ERROR;
            $returnData['msg'] = '签名验证错误';
            return collectionResp($returnData);
        }
        $checkParam = $this->verifyParam($request->route()->action,$pubData);
        if($checkParam['result'] !== CollectionApiRequest::RESPONSE_OK){
            return collectionResp($checkParam);
        }
        return $next($request);

    }

    protected function verifySign(array $data, $sign)
    {
        $publicKey = base_path(). '/privateKey/collection/passive/rsa_public_key.pem';
        $pubKey = file_get_contents($publicKey);
        $res = openssl_get_publickey($pubKey);
        ArrayTools::recursive_key_sort($data);
        $data = json_encode($data,JSON_UNESCAPED_UNICODE);
        $sign = str_replace(' ', '+', $sign);
        $result = openssl_verify($data, base64_decode($sign), $res);
        openssl_free_key($res);
        return $result;
    }




    protected function verifyParam($action,$pubData){
        $returnData['result'] = CollectionApiRequest::RESPONSE_PARAM_NOT_FAND;
        $returnData['msg'] = '参数验证不通过';
        switch ($action['as']){
            case 'collection.withholding':
                //验证代扣的请求参数
                return $this->verifyWithholding($pubData);
                break;
            case 'collection.repayment':
                //验证全部还清的请求参数
                return $this->verifyRepayment($pubData);
                break;

            case 'collection.repayrelief.add':
                // 申请减免
                return $this->verifyRepayReliefAdd($pubData);
                break;
            case 'collection.writeoff':
                return $this->verifyPayCompensate($pubData);;
            default:
                break;
        }
        return $returnData;
    }

    /**
     * Description:验证代扣参数
     * Author:lijiafei
     * @param $data array
     * @return mixed
     */
    public function verifyWithholding($data){
        $caseCode = isset($data['caseCode']) ? $data['caseCode'] : '';
        $period = isset($data['period']) ? $data['period'] : -1;
        $type = isset($data['type']) ? $data['type'] : -1;
        $applyNumber = isset($data['applyNumber']) ? $data['applyNumber'] : '';
        if(empty($caseCode)){
            $returnData['result'] = CollectionApiRequest::RESPONSE_PARAM_NOT_FAND;
            $returnData['msg'] = '借款号不存在';
            return $returnData;
        }
        if($period == -1){
            $returnData['result'] = CollectionApiRequest::RESPONSE_PARAM_NOT_FAND;
            $returnData['msg'] = '期数不存在';
            return $returnData;
        }

        if($type == -1){
            $returnData['result'] = CollectionApiRequest::RESPONSE_PARAM_NOT_FAND;
            $returnData['msg'] = '业务类型不存在';
            return $returnData;
        }
        if(empty($applyNumber)){
            $returnData['result'] = CollectionApiRequest::RESPONSE_PARAM_NOT_FAND;
            $returnData['msg'] = '申请流水号不存在';
            return $returnData;
        }
        $returnData['result'] = CollectionApiRequest::RESPONSE_OK;
        $returnData['msg'] = 'ok';
        return $returnData;
    }

    protected function verifyRepayment($data){
        return $this->verifyWithholding($data);//现阶段参数一样
    }

    /**
     * Description: 验证还款减免参数
     * Author: lyh
     * @param $data array
     * @return mixed
     */
    protected function verifyRepayReliefAdd($data)
    {
        $caseCode = isset($data['caseCode']) ? $data['caseCode'] : '';
        $period = isset($data['period']) ? $data['period'] : -1;
        $relief_amount = isset($data['relief_amount']) ? $data['relief_amount'] : 0;

        if (empty($caseCode)) {
            $returnData['result'] = CollectionApiRequest::RESPONSE_PARAM_NOT_FAND;
            $returnData['msg'] = '借款账号不存在';
        }

        if($period == -1) {
            $returnData['result'] = CollectionApiRequest::RESPONSE_PARAM_NOT_FAND;
            $returnData['msg'] = '期数不存在';
        }

        if ($relief_amount <= 0) {
            $returnData['result'] = CollectionApiRequest::RESPONSE_PARAM_NOT_FAND;
            $returnData['msg'] = '还款减免金额需大于0';
        }

        $returnData['result'] = CollectionApiRequest::RESPONSE_OK;
        $returnData['msg'] = 'ok';
        return $returnData;
    }

    /**
     * 商户代偿(销账)接口参数验证
     *
     * @author lyh
     * @param $data array
     * @return mixed array
     */
    protected function verifyPayCompensate($data)
    {
        $caseCode = isset($data['caseCode']) ? $data['caseCode'] : '';
        $period = isset($data['period']) ? $data['period'] : -1;
        $type = isset($data['type']) ? $data['type'] : -1;
        $repayAmount = isset($data['repayAmount']) ? $data['repayAmount'] : 0;
        $applyNumber = isset($data['applyNumber']) ? $data['applyNumber'] : '';
        if(empty($caseCode)){
            $returnData['result'] = CollectionApiRequest::RESPONSE_PARAM_NOT_FAND;
            $returnData['msg'] = '借款号不存在';
            return $returnData;
        }

        if($period == -1){
            $returnData['result'] = CollectionApiRequest::RESPONSE_PARAM_NOT_FAND;
            $returnData['msg'] = '期数不存在';
            return $returnData;
        }
        if($type == -1){
            $returnData['result'] = CollectionApiRequest::RESPONSE_PARAM_NOT_FAND;
            $returnData['msg'] = '业务类型不存在';
            return $returnData;
        }
        if(empty($applyNumber)){
            $returnData['result'] = CollectionApiRequest::RESPONSE_PARAM_NOT_FAND;
            $returnData['msg'] = '申请流水号不存在';
            return $returnData;
        }

        if(empty($repayAmount)){
            $returnData['result'] = CollectionApiRequest::RESPONSE_PARAM_NOT_FAND;
            $returnData['msg'] = '销账金额不存在';
            return $returnData;
        }

        $returnData['result'] = CollectionApiRequest::RESPONSE_OK;
        $returnData['msg'] = 'ok';
        return $returnData;
    }

}
